﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace LunwenYun.Infrastructure.Entities.Repository
{
    [Table("T_PaperAgentInfo")]
    public class T_PaperAgentInfo : IEntity<T_PaperAgentInfo>
    {
        #region
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        [Key]
        public string Username { get; set; }
        /// <summary>
        /// 约稿人称呼
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string Name { get; set; }

        [Column(TypeName = "text")]
        public string Introduction { get; set; }

        public int AccountType { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string AccountNo { get; set; }

        /// <summary>
        /// 约稿声明
        /// </summary>
        [Column(TypeName = "text")]
        public string Declaration { get; set; }

        [Column(TypeName = "varchar")]
        [StringLength(64)]
        public string QQ { get; set; }

        [Column(TypeName = "varchar")]
        [StringLength(64)]
        public string Phone { get; set; }

        /// <summary>
        /// 接收推送邮箱
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(256)]
        public string Email { get; set; }
        
        public DateTime RcdDate { get; set; }
        public DateTime RefreshDate { get; set; }
        #endregion

        public override T_PaperAgentInfo Find(string key)
        {
            return Find(c => c.Username == key);
        }

        public override T_PaperAgentInfo Find(System.Linq.Expressions.Expression<Func<T_PaperAgentInfo, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.PaperAgentInfo.FirstOrDefault(where);
                if (item != null)
                {
                    this.Username = item.Username;
                    this.AccountNo = item.AccountNo;
                    this.AccountType = item.AccountType;
                    this.Declaration = item.Declaration;
                    this.Email = item.Email;
                    this.Introduction = item.Introduction;
                    this.Name = item.Name;
                    this.Phone = item.Phone;
                    this.QQ = item.QQ;
                    this.RcdDate = item.RcdDate;
                    this.RefreshDate = item.RefreshDate;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_PaperAgentInfo, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.PaperAgentInfo.Any(where);
            }
        }
    }
}
